package com.web.cms.doc.provider;

import com.blue.core.dict.State;
import com.blue.core.tree.TreeBuilder;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.cms.doc.cache.ChannelCache;
import com.web.cms.doc.exception.ChannelException;
import com.web.cms.doc.facade.ChannelFacade;
import com.web.cms.doc.message.ChannelRequest;
import com.web.cms.doc.message.ChannelResponse;
import com.web.cms.doc.model.Channel;
import com.web.cms.doc.service.ChannelService;
import com.web.cms.doc.util.ChannelConvertor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component("cmsChannelProvider")
public class ChannelProvider implements ChannelFacade
{
	@Autowired
	private ChannelService channelService;
	@Autowired
	private ChannelCache channelCache;
	
	public ChannelProvider()
	{
	}

	@Override
	public ChannelResponse add(ChannelRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		Channel channel = ChannelConvertor.toChannel(request);
		channel = channelService.save(channel);
		channelCache.delete();
		ChannelResponse response = ChannelConvertor.toResponse(channel);
		return response;
	}

	@Override
	public ChannelResponse update(ChannelRequest request)
	{
		ValidationUtil.valid(request, UpdateModel.class);
		Channel channel = ChannelConvertor.toChannel(request);
		channel = channelService.save(channel);
		channelCache.delete();
		ChannelResponse response = ChannelConvertor.toResponse(channel);
		return response;
	}

	@Override
	public ChannelResponse get(Integer id)
	{
		if (id == null)
			throw new ChannelException("栏目ID不能为空");
			
		Channel channel = channelCache.get(id);
		ChannelResponse response = ChannelConvertor.toResponse(channel);
		return response;
	}

	@Override
	public List<ChannelResponse> list(Integer webId, Integer parentId, State state)
	{
		if (webId == null)
			throw new ChannelException("网站ID不能为空");

		List<Channel> list = null;
		if (state == null)
		{
			Channel channel = new Channel();
			channel.setWebId(webId);
			channel.setParentId(parentId == null ? 0 : parentId);
			list = channelService.list(channel);
		}
		else if (state == State.NORMAL)
		{
			list = channelCache.listByWebAndParent(webId, parentId);
		}
		List<ChannelResponse> responseList = ChannelConvertor.toResponse(list);
		return responseList;
	}

	@Override
	public Map<String, List<ChannelResponse>> listTree(Integer webId, State state)
	{
		if (webId == null)
			throw new ChannelException("网站ID不能为空");

		Map<Integer, List<Channel>> map = null;
		if (state == null)
		{
			Channel channel = new Channel();
			channel.setWebId(webId);
			List<Channel> list = channelService.list(channel);
			map = TreeBuilder.buildTree(list);
		}
		else if (state == State.NORMAL)
		{
			map = channelCache.listByWeb(webId);
		}
		Map<String, List<ChannelResponse>> responseMap = ChannelConvertor.toTree(map);
		return responseMap;
	}
	
	@Override
	public void refresh()
	{
		channelCache.delete();
	}
	
}
